﻿using System.Collections.Generic;
using UnityEngine;

/****************************************************
// 功能：Buff 逻辑
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/4/9 09:56:23
*****************************************************/

public class Buff : SubLogicUnit {
    /// <summary>
    /// buff附着单位
    /// </summary>
    public MainLogicUnit owner;
    protected int buffID;
    protected object[] args;

    // buff持续时间
    protected int buffDuration;
    private int tickCount = 0;//Dot计数
    private int durationCount = 0;//时长计时
    public BuffCfg cfg;

    /// <summary>
    /// 群体buff作用目标列表
    /// </summary>
    protected List<MainLogicUnit> targetList;
    // buff表现层
    private BuffView buffView;

    public Buff(MainLogicUnit source, MainLogicUnit owner, Skill skill, int buffID, object[] args = null)
        : base(source, skill) {
        this.owner = owner;
        this.buffID = buffID;
        this.args = args;
    }

    public override void LogicInit() {
        cfg = ResourceManager.Instance.GetBuffConfigById(buffID);
        buffDuration = cfg.buffDuration;
        delayTime = cfg.buffDelay;

        base.LogicInit();
    }

    public override void LogicTick() {
        base.LogicTick();
        switch(unitState) {
            case SubUnitState.Start:
                Start();
                unitState = buffDuration is > 0 or -1 ? SubUnitState.Tick : SubUnitState.End;
                break;
            case SubUnitState.Tick:
                if(cfg.buffInterval > 0)
                {
                    tickCount += Constants.SERVER_LOGIC_FRAME_ID;
                    if(tickCount >= cfg.buffInterval) {
                        tickCount -= cfg.buffInterval;
                        Tick();
                    }
                }
                durationCount += Constants.SERVER_LOGIC_FRAME_ID;
                if(durationCount >= buffDuration && buffDuration != -1) {
                    unitState = SubUnitState.End;
                }
                break;
        }
    }

    protected override void Start() {
        //根据staticPosType决定Buff初始位置
        if(cfg.staticPosType == StaticPosTypeEnum.None) {
            LogicPos = owner.LogicPos;
        }

        if(cfg.buffEffect != null) {
            GameObject go = ResourceManager.Instance.LoadPrefab("Effects/" + cfg.buffEffect);
            go.name = source.unitName + "_" + cfg.buffName;
            buffView = go.GetComponent<BuffView>();
            if(buffView == null) {
                Debug.LogError("Get BuffView Error:" + unitName);
            }

            if(cfg.staticPosType == StaticPosTypeEnum.None) {
                owner.mainViewUnit.SetBuffFollower(buffView);
            }

            if (buffView == null) return;
            buffView.Init(this);

            if (cfg.buffAudio != null)
            {
                buffView.PlayAudio(cfg.buffAudio);
            }
        }
        else {
            if(cfg.buffAudio != null) {
                owner.PlayAudio(cfg.buffAudio);
            }
        }
    }
    protected override void Tick() {
        if(cfg.hitTickAudio != null && targetList.Count > 0) {
            owner.PlayAudio(cfg.hitTickAudio);
        }
    }
    protected override void End() {
        if(cfg.buffEffect != null) {
            buffView.DestroyBuff();
        }
    }
}
